package client.OCSF;

import java.io.*;



public class BillSysClient extends AbstractClient {
	//Instance variables **********************************************

	  /**
	   * The interface type variable.  It allows the implementation of
	   * the display method in the client.
	   */
	  BillSysIF billsysUI;

	  //Constructors ****************************************************

	  /**
	   * Constructs an instance of the chat client.
	   *
	   * @param host The server to connect to.
	   * @param port The port number to connect on.
	   * @param clientUI The interface type variable.
	   */

	  public BillSysClient(String host, int port, BillSysIF clientUI)
	    throws IOException
	  {
	    super(host, port); //Call the superclass constructor
	    this.billsysUI = clientUI;
	    openConnection();
	  }
	  

	  //Instance methods ************************************************

	  /**
	   * This method handles all data that comes in from the server.
	   *
	   * @param msg The message from the server.
	   */
	  public void handleMessageFromServer(Object msg)
	  {
	    billsysUI.display(msg.toString());
	 
	  }
	  

/*	  private void printXMLConvert()
		{
		  JAXBContext context;
		try {
			context = JAXBContext.newInstance(Tickets.class);
			System.out.println("Output from the XML");
			Unmarshaller um = context.createUnmarshaller();
			ticket = (Tickets) um.unmarshal(reader);
			
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
			for (int i = 0; i <ticket.getTicket().toArray().length; i++){
				System.out.println("Ticket" + (i+1) +  " " +ticket.getTicket().get(i).getTkName()+
						" " + ticket.getTicket().get(i).getTkDescription() + "----" + 
						ticket.getTicket().get(i).getTkPrice());
			}
			
		}*/

	  /**
	   * This method handles all data coming from the UI
	   *
	   * @param message The message from the UI.
	   */
	  public void handleMessageFromClientUI(String message)
	  {
	    try
	    {
	      sendToServer(message);
	    }
	    catch(IOException e)
	    {
	    /*  sensorUI.display
	        ("ERROR - Message could not be sent to the server. DISCONNECTED.");
	      quit();*/
	    }
	  }

	  /**
	   * Displays a message on screen
	   */
	  public void connectionClosed() {

		  billsysUI.display("CONNECTION CLOSED @BillSysClient.java");
	  }

	  /**
	   * This method terminates the client.
	   */
	  public void quit()
	  {
	    try
	    {
	      closeConnection();
	    }
	    catch(IOException ex) {}
	    billsysUI.display("ERROR - Error occurred while quiting");
	  }

}
